package router

import (
	"net/http"
	"woanju/controller"
	//_ "nil/docs" // 千万不要忘了导入把你上一步生成的docs
	"woanju/logger"
	"woanju/middlewares"

	"github.com/gin-gonic/gin"

	"github.com/gin-contrib/pprof"
)

func SetupRouter(mode string) *gin.Engine {
	if mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.New()
	//r.Use(logger.GinLogger(), logger.GinRecovery(true), middlewares.RateLimitMiddleware(2*time.Second, 1))
	r.Use(logger.GinLogger(), logger.GinRecovery(true))

	//r.LoadHTMLFiles("templates/index.html")
	//r.Static("/static", "./static")
	//r.GET("/", func(c *gin.Context) {
	//	c.HTML(http.StatusOK, "index.html", nil)
	//})

	//r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))

	//注册业务路由
	v1 := r.Group("/api/v1")
	{
		//注册
		v1.POST("/signup", controller.SignUpHandler)

		//发送激活邮件
		v1.POST("/captcha", controller.CaptchaHandler)

		//登录
		v1.POST("/login", controller.LoginHandler)

	}

	v1.Use(middlewares.JWTAuthMiddleware()) //应用JWT认证中间件
	{
		//获取用户信息
		v1.GET("/user", controller.UserInfoHandler)

		//修改用户信息
		v1.POST("/user/update", controller.UpdateUserInfoHandler)

		//添加/更改监护人信息
		v1.POST("/guardian", controller.GuardianHandler)

		//添加设备
		v1.POST("/equipment", controller.EquipmentHandler)

		//删除设备
		v1.GET("/equipment/:id", controller.DeleteEquipmentHandler)

		//更改设备状态
		v1.POST("/equipment/status", controller.UpdateStatusEquipmentHandler)

		//获取设备信息
		v1.GET("/equipments", controller.EquipmentListHandler)

		//发送警报
		v1.GET("/warn", controller.WarnHandler)

	}

	pprof.Register(r) //注册pprof相关路由

	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "404",
		})
	})

	return r
}
